<?php
class M_users extends CI_Model {
	
	private $last_insert_id;
	/* 	
		-------------------------------------------------------------------
		add_user
		12-6-12
		perry
		-------------------------------------------------------------------
		Adds a user on the system
		
		Returns true if add success
		Returns false if add unsuccessful
	*/
	public function add_user($attrib){
		$v = (object) $attrib;
		$hashed_password = $this->hash_password($v->hashed_password);
		
		$user_data = array(
				'username' => $v->username,
				'hashed_password' => $hashed_password,
				'userType' => $v->userType,
				'userStatus' => $v->userStatus,
				'created_at' => NOW,
		);
		$this->db->insert('users', $user_data);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	
	
	/* 	
		-------------------------------------------------------------------
		verify_user
		12-6-12
		perry
		-------------------------------------------------------------------
		 Verifies if an existing user is present in the system and checks their login credentials
		
		Returns false if credentials are invalid or userstatus is not active
		Returns true if credentials are valid
	*/
	public function verify_user($attrib)
	{
		$v = (object) $attrib;
		$this->db->where('username', $v->username);
		$query = $this->db->get('users');
	
		if($query->num_rows() >= 1 && $query->row()->userStatus == 'active')
		{
			$row = $query->row();
			
			if($this->validate_password($row->hashed_password, $v->password)){
				$userdata = array(
					'userid' => $row->id,
					'username' => $row->username,
					'userType' => $row->userType,
					'logged_in' => TRUE
				);
				
				$this->session->set_userdata($userdata);
				return true;
			}
		}else{
			return false;
		}
	}
	
	
	/* 	
		-------------------------------------------------------------------
		hash_password
		12-6-12
		perry
		-------------------------------------------------------------------
		hashes the users password
		
		returns the hashed password with salt
	*/
	public function hash_password($password)
    {
        $salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
        $hash = hash('sha256', $salt . $password);

        return $salt . $hash;
    }

	/* 	
		-------------------------------------------------------------------
		validate_password
		12-6-12
		perry
		-------------------------------------------------------------------
		validates the users password
		
		returns the hashed password with salt
	*/
	public function validate_password($hashed_password, $password)
    {
        $salt = substr($hashed_password, 0, 64);
        $hash = substr($hashed_password, 64, 64);

        $password_hash = hash('sha256', $salt . $password);
        
        return $password_hash == $hash;
    }
	
	/*
	--------------------------------------------------------------------------
	get_all_users
	12-6-2012
	tom
	--------------------------------------------------------------------------
	@param id
	if parameter is false gets all users from db
	if parameter exists gets specific id from parameter
	*/
	
	
	
	public function get_all_users($id = FALSE)
	{
		if($id == FALSE){
			$query = $this->db->get('users');		
		}else{
			$query = $this->db->where('id',$id)->get('users');	
		}
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
	/*
	--------------------------------------------------------------------------
	update_users
	12-6-2012
	tom
	--------------------------------------------------------------------------
	@param id
	@param data = array
	updates user info
	*/	
	
	public function update_user($id,$data)
	{
		$this->db->set($data)->where('id',$id)->update('users');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
	

	/*
	--------------------------------------------------------------------------
	change_pass
	12-6-2012
	tom
	--------------------------------------------------------------------------
	@param id
	@param newpass
	updates password
	*/	
	
	public function change_pass($newpassword,$id){

			$newpassword = $this->hash_password($newpassword);
			if($this->db->set('hashed_password',$newpassword)->where('id',$id)->update('users')){
				return TRUE;
			}else{
				return FALSE;
			}
	}
	
	/*
	--------------------------------------------------------------------------
	verify_password
	12-6-2012
	tom
	--------------------------------------------------------------------------
	@param id
	@param old password
	checks password from database and matches from user input
	*/	
	public function verify_password($password,$id)
	{
		$data = $this->db->select('hashed_password')->where('id',$id)->get('users');
		foreach($data->result() as $data)
		{
			$h_pass = $data->hashed_password;
			break;
		}
		if($this->validate_password($h_pass,$password)){
			return TRUE;	
		}else{
			return FALSE;
		}
	}

		
	public function destroy($id)
	{
		$this->db->update('status','inactive')->where('id',$id)->update('users');
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;
	}
		
	
	function delete_user($user_id){
		$this->db->where('id', $user_id)->delete('users');
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function register_complete_info($profile_data)
	{
		unset($profile_data['level_id']);
		unset($profile_data['student_number']);
		$profile_data = (object)$profile_data;
		
		//format date and get age from birhtdate
		$d = $this->__format($profile_data->child_bdate);
		$profile_input['email'] = $profile_data->email;
		$profile_input['firstname'] = $profile_data->child_fname;
		$profile_input['middlename'] = strtolower($profile_data->child_mname) == 'none' ? NULL : $profile_data->child_mname;
		$profile_input['lastname'] = $profile_data->child_lname;
		$profile_input['nickname'] = $profile_data->child_nickname;
		$profile_input['name_ext'] = $profile_data->child_name_ext;
		$profile_input['gender'] = $profile_data->gender;
		$profile_input['dob'] = $d->dob;
		$profile_input['age'] = $d->age;
		if($profile_data->child_nationality == 'other'){
			$profile_input['nationality'] = $profile_data->child_nationality2;
		}else{
			$profile_input['nationality'] = $profile_data->child_nationality;
		}
		if($profile_input['nationality']=='filipino' || $profile_input['nationality']=='filipina' || $profile_input['nationality']=='pilipino' || $profile_input['nationality']=='pilipina' || $profile_input['nationality']=='philippine' || $profile_input['nationality']=='fil' || $profile_input['nationality']=='pinoy' || $profile_input['nationality']=='pinay' || $profile_input['nationality']=='philippines' || $profile_input['nationality']=='phil' || $profile_input['nationality']=='fhil' || $profile_input['nationality']=='pnoy'):
			$profile_input['nationality']='Filipino';
		endif;
		$profile_input['pob'] = $profile_data->child_placeofbirth;
		$profile_input['present_address'] = $profile_data->child_city_address;
		$profile_input['contact_no'] = $profile_data->child_telno;
		$profile_input['contact_no'] = $profile_data->mobile_phone;
		$profile_input['religion'] = $profile_data->child_religous;
		$profile_input['living_in_ph_with_father'] = !isset($profile_data->living_in_ph_with_father) ? 'no' : 'yes';
		$profile_input['living_in_ph_with_mother'] = !isset($profile_data->living_in_ph_with_mother) ? 'no' : 'yes';
		$profile_input['living_in_ph_with_guardian'] = !isset($profile_data->living_in_ph_with_guardian) ? 'no' : 'yes';
		$profile_input['childs_english_reading_writing_level'] = $profile_data->childs_english_reading_writing_level;
		$profile_input['childs_english_verbal_proficiency'] = $profile_data->childs_english_verbal_proficiency;
		$profile_input['language_at_home'] = $profile_data->language_at_home;
		$profile_input['participation_in_advanced_level_classes'] = $profile_data->participation_in_advanced_level_classes;
		$profile_input['remedial_help_in_previous_schools'] = $profile_data->remedial_help_in_previous_schools;
		$profile_input['special_talent_interest_dance'] = !isset($profile_data->special_talent_interest_dance) ? 'no' : 'yes';
		$profile_input['special_talent_interest_drama'] = !isset($profile_data->special_talent_interest_drama) ? 'no' : 'yes';
		$profile_input['special_talent_interest_art'] = !isset($profile_data->special_talent_interest_art) ? 'no' : 'yes';
		$profile_input['special_talent_interest_acomputer'] = !isset($profile_data->special_talent_interest_acomputer) ? 'no' : 'yes';
		$profile_input['special_talent_interest_vocal_chorus'] = !isset($profile_data->special_talent_interest_vocal_chorus) ? 'no' : 'yes';
		$profile_input['special_talent_interest_athletics'] = !isset($profile_data->special_talent_interest_athletics) ? 'no' : 'yes';
		$profile_input['special_talent_interest_athletics_spec'] = $profile_data->special_talent_interest_athletics_spec;
		$profile_input['special_talent_interest_band'] = !isset($profile_data->special_talent_interest_band) ? 'no' : 'yes';
		$profile_input['special_talent_interest_band_spec'] = $profile_data->special_talent_interest_band_spec;
		$profile_input['special_talent_interest_string'] = !isset($profile_data->special_talent_interest_string) ? 'no' : 'yes';
		$profile_input['special_talent_interest_string_spec'] = $profile_data->special_talent_interest_string_spec;
		$profile_input['dhc_hinder_learning_process'] = $profile_data->hinder_childs_learning;
		$profile_input['leave_school'] = $profile_data->leave_school;
		$profile_input['previous_school'] = $profile_data->previous_school;
		$profile_input['previous_school_address'] = $profile_data->previous_school_address;
		$profile_input['previous_grade_level'] = $profile_data->previous_grade_level;
		$profile_input['guardian_name'] = $profile_data->guardian_name;
		$profile_input['guardian_mname'] = $profile_data->guardian_mname;
		$profile_input['guardian_lname'] = $profile_data->guardian_lname;
		$profile_input['guardian_citizenship'] = $profile_data->guardian_citizenship;
		$profile_input['guardian_visa'] = $profile_data->guardian_visa;
		$profile_input['guardian_occup'] = $profile_data->guardian_occup;
		$profile_input['guardian_office_tel'] = $profile_data->guardian_office_tel;
		$profile_input['guardian_contact_no'] = $profile_data->guardian_contact_no;
		$profile_input['guardian_contact_no_text'] = !isset($profile_data->guardian_contact_no_text) ? 'no' : 'yes';
		$profile_input['fathername'] = $profile_data->father_name;
		$profile_input['father_mname'] = $profile_data->father_mname;
		$profile_input['father_lname'] = $profile_data->father_lname;
		$profile_input['father_nationality'] = $profile_data->father_citizenship;
		$profile_input['father_visa'] = $profile_data->father_visa;
		$profile_input['father_occupation'] =$profile_data->father_occup;
		$profile_input['father_office_num'] =$profile_data->father_office_tel;
		$profile_input['father_contact_no'] =$profile_data->father_contact_no;
		$profile_input['father_contact_no_text'] = !isset($profile_data->father_contact_no_text) ? 'no' : 'yes';
		$profile_input['mothername'] =$profile_data->mother_name;
		$profile_input['mother_mname'] =$profile_data->mother_mname;
		$profile_input['mother_lname'] =$profile_data->mother_lname;
		$profile_input['mother_nationality'] =$profile_data->mother_citizenship;
		$profile_input['mother_visa'] =$profile_data->mother_visa;
		$profile_input['mother_occupation'] =$profile_data->mother_occup;
		$profile_input['mother_office_num'] =$profile_data->mother_office_tel;
		$profile_input['mother_contact_no'] =$profile_data->mother_contact_no;
		$profile_input['mother_contact_no_text'] = !isset($profile_data->mother_contact_no_text) ? 'no' : 'yes';
		$profile_input['step_father_name'] =$profile_data->step_father_name;
		$profile_input['step_father_mname'] = $profile_data->step_father_mname;
		$profile_input['step_father_lname'] = $profile_data->step_father_lname;
		$profile_input['step_father_citizenship'] =$profile_data->step_father_citizenship;
		$profile_input['step_father_visa'] =$profile_data->step_father_visa;
		$profile_input['step_father_occup'] =$profile_data->step_father_occup;
		$profile_input['step_father_office_tel'] =$profile_data->step_father_office_tel;
		$profile_input['step_father_contact_no'] =$profile_data->step_father_contact_no;
		$profile_input['step_father_contact_no_text'] = !isset($profile_data->step_father_contact_no_text) ? 'no' : 'yes';
		$profile_input['step_mother_name'] =$profile_data->step_mother_name;
		$profile_input['step_mother_mname'] = $profile_data->step_mother_mname;
		$profile_input['step_mother_lname'] = $profile_data->step_mother_lname;
		$profile_input['step_mother_citizenship'] =$profile_data->step_mother_citizenship;
		$profile_input['step_mother_visa'] =$profile_data->step_mother_visa;
		$profile_input['step_mother_occup'] =$profile_data->step_mother_occup;
		$profile_input['step_mother_office_tel'] =$profile_data->step_mother_office_tel;
		$profile_input['step_mother_contact_no'] =$profile_data->step_mother_contact_no;
		$profile_input['step_mother_contact_no_text'] = !isset($profile_data->step_mother_contact_no_text) ? 'no' : 'yes';
		$profile_input['failure_to_provide'] = !isset($profile_data->failure_to_provide) ? 'no' : 'yes';
		$profile_input['to_the_best'] = !isset($profile_data->to_the_best) ? 'no' : 'yes';
		$profile_input['hhc_allergies_reaction'] = $profile_data->allergies_reaction;
		$profile_input['hhc_alergic_med_reaction'] = $profile_data->drug_allergies_reaction;
		$profile_input['dhc_sight_problems_what'] = $profile_data->eye_problem_spec;
		$profile_input['dhc_hearing_problems_what'] = $profile_data->ear_problem_spec;
		$profile_input['hhc_diagnosed_specify'] = $profile_data->major_ailment;
		$profile_input['hhc_recently_hospitalized'] = $profile_data->recently_hospitalized;
		$profile_input['hhc_major_injury_specify'] = $profile_data->major_injury;
		$profile_input['hhc_medication_school_hours'] = $profile_data->medication_child_taking_spec;
		$profile_input['hhc_asthma_inhaler'] = isset($profile_data->asthma_inhaler) ? $profile_data->asthma_inhaler : 'no';
		$profile_input['hhc_asthma'] = isset($profile_data->asthma) ? $profile_data->asthma : 'no';
		$profile_input['auth_firstaid'] = $profile_data->authfirst_aid;
		$profile_input['auth_emergency_care'] = $profile_data->auth_emergency;
		$profile_input['auth_emergency_care_hospital'] = $profile_data->auth_emergency_nearest_hosp;
		$profile_input['auth_oral_non_presc'] = $profile_data->auth_oral_non_presc;
		$profile_input['first_permission'] = isset($profile_data->first_permission) ? 'yes' : 'no';
		$profile_input['second_permission'] = isset($profile_data->second_permission) ? 'yes' : 'no';
		$profile_input['third_permission'] = isset($profile_data->third_permission) ? 'yes' : 'no';
		
		$profile_input['created'] = NOW;
		
		$this->db->insert('profiles',$profile_input);
		$this->last_insert_id = $this->db->insert_id();
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	public function update_complete_info($profile_data,$e_profile_id)
	{
		unset($profile_data['level_id']);
		unset($profile_data['student_number']);
		$profile_data = (object)$profile_data;
		
		//format date and get age from birhtdate
		$d = $this->__format($profile_data->child_bdate);
		$profile_input['email'] = $profile_data->email;
		$profile_input['firstname'] = strtolower($profile_data->child_fname);
		$profile_input['middlename'] = strtolower($profile_data->child_mname) == 'none' ? NULL : strtolower($profile_data->child_mname);
		$profile_input['lastname'] = strtolower($profile_data->child_lname);
		$profile_input['nickname'] = strtolower($profile_data->child_nickname);
		$profile_input['name_ext'] = strtolower($profile_data->child_name_ext);
		$profile_input['gender'] = $profile_data->gender;
		$profile_input['dob'] = $d->dob;
		$profile_input['age'] = $d->age;
		if($profile_data->child_nationality == 'other'){
			$profile_input['nationality'] = $profile_data->child_nationality2;
		}else{
			$profile_input['nationality'] = $profile_data->child_nationality;
		}
		if($profile_input['nationality']=='filipino' || $profile_input['nationality']=='filipina' || $profile_input['nationality']=='pilipino' || $profile_input['nationality']=='pilipina' || $profile_input['nationality']=='philippine' || $profile_input['nationality']=='fil' || $profile_input['nationality']=='pinoy' || $profile_input['nationality']=='pinay' || $profile_input['nationality']=='philippines' || $profile_input['nationality']=='phil' || $profile_input['nationality']=='fhil' || $profile_input['nationality']=='pnoy'):
			$profile_input['nationality']='Filipino';
		endif;
		$profile_input['pob'] = $profile_data->child_placeofbirth;
		$profile_input['present_address'] = $profile_data->child_city_address;
		$profile_input['contact_no'] = $profile_data->child_telno;
		$profile_input['contact_no'] = $profile_data->mobile_phone;
		$profile_input['religion'] = $profile_data->child_religous;
		$profile_input['living_in_ph_with_father'] = !isset($profile_data->living_in_ph_with_father) ? 'no' : 'yes';
		$profile_input['living_in_ph_with_mother'] = !isset($profile_data->living_in_ph_with_mother) ? 'no' : 'yes';
		$profile_input['living_in_ph_with_guardian'] = !isset($profile_data->living_in_ph_with_guardian) ? 'no' : 'yes';
		$profile_input['childs_english_reading_writing_level'] = $profile_data->childs_english_reading_writing_level;
		$profile_input['childs_english_verbal_proficiency'] = $profile_data->childs_english_verbal_proficiency;
		$profile_input['language_at_home'] = $profile_data->language_at_home;
		$profile_input['participation_in_advanced_level_classes'] = $profile_data->participation_in_advanced_level_classes;
		$profile_input['remedial_help_in_previous_schools'] = $profile_data->remedial_help_in_previous_schools;
		$profile_input['special_talent_interest_dance'] = !isset($profile_data->special_talent_interest_dance) ? 'no' : 'yes';
		$profile_input['special_talent_interest_drama'] = !isset($profile_data->special_talent_interest_drama) ? 'no' : 'yes';
		$profile_input['special_talent_interest_art'] = !isset($profile_data->special_talent_interest_art) ? 'no' : 'yes';
		$profile_input['special_talent_interest_acomputer'] = !isset($profile_data->special_talent_interest_acomputer) ? 'no' : 'yes';
		$profile_input['special_talent_interest_vocal_chorus'] = !isset($profile_data->special_talent_interest_vocal_chorus) ? 'no' : 'yes';
		$profile_input['special_talent_interest_athletics'] = !isset($profile_data->special_talent_interest_athletics) ? 'no' : 'yes';
		$profile_input['special_talent_interest_athletics_spec'] = $profile_data->special_talent_interest_athletics_spec;
		$profile_input['special_talent_interest_band'] = !isset($profile_data->special_talent_interest_band) ? 'no' : 'yes';
		$profile_input['special_talent_interest_band_spec'] = $profile_data->special_talent_interest_band_spec;
		$profile_input['special_talent_interest_string'] = !isset($profile_data->special_talent_interest_string) ? 'no' : 'yes';
		$profile_input['special_talent_interest_string_spec'] = $profile_data->special_talent_interest_string_spec;
		$profile_input['dhc_hinder_learning_process'] = $profile_data->hinder_childs_learning;
		$profile_input['leave_school'] = $profile_data->leave_school;
		$profile_input['previous_school'] = $profile_data->previous_school;
		$profile_input['previous_school_address'] = $profile_data->previous_school_address;
		$profile_input['previous_grade_level'] = $profile_data->previous_grade_level;
		$profile_input['guardian_name'] = $profile_data->guardian_name;
		$profile_input['guardian_mname'] = strtolower($profile_data->guardian_mname) == 'none' ? NULL : strtolower($profile_data->guardian_mname);
		$profile_input['guardian_lname'] = $profile_data->guardian_lname;
		$profile_input['guardian_citizenship'] = $profile_data->guardian_citizenship;
		$profile_input['guardian_visa'] = $profile_data->guardian_visa;
		$profile_input['guardian_occup'] = $profile_data->guardian_occup;
		$profile_input['guardian_office_tel'] = $profile_data->guardian_office_tel;
		$profile_input['guardian_contact_no'] = $profile_data->guardian_contact_no;
		$profile_input['guardian_contact_no_text'] = !isset($profile_data->guardian_contact_no_text) ? 'no' : 'yes';
		$profile_input['fathername'] = strtolower($profile_data->father_name);
		$profile_input['father_mname'] = strtolower($profile_data->father_mname) == 'none' ? NULL : strtolower($profile_data->father_mname);
		$profile_input['father_lname'] = strtolower($profile_data->father_lname);
		$profile_input['father_nationality'] = $profile_data->father_citizenship;
		$profile_input['father_visa'] = $profile_data->father_visa;
		$profile_input['father_occupation'] =$profile_data->father_occup;
		$profile_input['father_office_num'] =$profile_data->father_office_tel;
		$profile_input['father_contact_no'] =$profile_data->father_contact_no;
		$profile_input['father_contact_no_text'] = !isset($profile_data->father_contact_no_text) ? 'no' : 'yes';
		$profile_input['mothername'] =strtolower($profile_data->mother_name);
		$profile_input['mother_mname'] =strtolower($profile_data->mother_mname) == 'none' ? NULL : strtolower($profile_data->mother_mname);
		$profile_input['mother_lname'] =strtolower($profile_data->mother_lname);
		$profile_input['mother_nationality'] =$profile_data->mother_citizenship;
		$profile_input['mother_visa'] =$profile_data->mother_visa;
		$profile_input['mother_occupation'] =$profile_data->mother_occup;
		$profile_input['mother_office_num'] =$profile_data->mother_office_tel;
		$profile_input['mother_contact_no'] =$profile_data->mother_contact_no;
		$profile_input['mother_contact_no_text'] = !isset($profile_data->mother_contact_no_text) ? 'no' : 'yes';
		$profile_input['step_father_name'] =strtolower($profile_data->step_father_name);
		$profile_input['step_father_mname'] = strtolower($profile_data->step_father_mname) == 'none' ? NULL : strtolower($profile_data->step_father_mname);
		$profile_input['step_father_lname'] = strtolower($profile_data->step_father_lname);
		$profile_input['step_father_citizenship'] =$profile_data->step_father_citizenship;
		$profile_input['step_father_visa'] =$profile_data->step_father_visa;
		$profile_input['step_father_occup'] =$profile_data->step_father_occup;
		$profile_input['step_father_office_tel'] =$profile_data->step_father_office_tel;
		$profile_input['step_father_contact_no'] =$profile_data->step_father_contact_no;
		$profile_input['step_father_contact_no_text'] = !isset($profile_data->step_father_contact_no_text) ? 'no' : 'yes';
		$profile_input['step_mother_name'] =strtolower($profile_data->step_mother_name);
		$profile_input['step_mother_mname'] = strtolower($profile_data->step_mother_mname) == 'none' ? NULL : strtolower($profile_data->step_mother_mname);
		$profile_input['step_mother_lname'] = strtolower($profile_data->step_mother_lname);
		$profile_input['step_mother_citizenship'] =$profile_data->step_mother_citizenship;
		$profile_input['step_mother_visa'] =$profile_data->step_mother_visa;
		$profile_input['step_mother_occup'] =$profile_data->step_mother_occup;
		$profile_input['step_mother_office_tel'] =$profile_data->step_mother_office_tel;
		$profile_input['step_mother_contact_no'] =$profile_data->step_mother_contact_no;
		$profile_input['step_mother_contact_no_text'] = !isset($profile_data->step_mother_contact_no_text) ? 'no' : 'yes';
		$profile_input['failure_to_provide'] = !isset($profile_data->failure_to_provide) ? 'no' : 'yes';
		$profile_input['to_the_best'] = !isset($profile_data->to_the_best) ? 'no' : 'yes';
		$profile_input['hhc_allergies_reaction'] = $profile_data->allergies_reaction;
		$profile_input['hhc_alergic_med_reaction'] = $profile_data->drug_allergies_reaction;
		$profile_input['dhc_sight_problems_what'] = $profile_data->eye_problem_spec;
		$profile_input['dhc_hearing_problems_what'] = $profile_data->ear_problem_spec;
		$profile_input['hhc_diagnosed_specify'] = $profile_data->major_ailment;
		$profile_input['hhc_recently_hospitalized'] = $profile_data->recently_hospitalized;
		$profile_input['hhc_major_injury_specify'] = $profile_data->major_injury;
		$profile_input['hhc_medication_school_hours'] = $profile_data->medication_child_taking_spec;
		$profile_input['hhc_asthma_inhaler'] = isset($profile_data->asthma_inhaler) ? $profile_data->asthma_inhaler : 'no';
		$profile_input['hhc_asthma'] = isset($profile_data->asthma) ? $profile_data->asthma : 'no';
		$profile_input['auth_firstaid'] = $profile_data->authfirst_aid;
		$profile_input['auth_emergency_care'] = $profile_data->auth_emergency;
		$profile_input['auth_emergency_care_hospital'] = $profile_data->auth_emergency_nearest_hosp;
		$profile_input['auth_oral_non_presc'] = $profile_data->auth_oral_non_presc;
		$profile_input['first_permission'] = isset($profile_data->first_permission) ? 'yes' : 'no';
		$profile_input['second_permission'] = isset($profile_data->second_permission) ? 'yes' : 'no';
		$profile_input['third_permission'] = isset($profile_data->third_permission) ? 'yes' : 'no';
		
		//$profile_input['created'] = NOW;
		
		$this->db->where('profile_id', $e_profile_id);
		$this->db->update('profiles', $profile_input); 
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function register_user($profile_data)
	{
		unset($profile_data['level_id']);
		$profile_data = (object)$profile_data;
		
		//format date and get age from birhtdate
		$d = $this->__format($profile_data->child_bdate);
		$profile_input['email'] = $profile_data->email;
		$profile_input['firstname'] = $profile_data->child_fname;
		$profile_input['middlename'] = strtolower($profile_data->child_mname) == 'none' ? NULL : $profile_data->child_mname;
		$profile_input['lastname'] = $profile_data->child_lname;
		$profile_input['nickname'] = $profile_data->child_nickname;
		$profile_input['name_ext'] = $profile_data->child_name_ext;
		$profile_input['gender'] = $profile_data->gender;
		$profile_input['dob'] = $d->dob;
		$profile_input['age'] = $d->age;
		if($profile_data->child_nationality == 'other'){
			$profile_input['nationality'] = $profile_data->child_nationality2;
		}else{
			$profile_input['nationality'] = $profile_data->child_nationality;
		}
		if($profile_input['nationality']=='filipino' || $profile_input['nationality']=='filipina' || $profile_input['nationality']=='pilipino' || $profile_input['nationality']=='pilipina' || $profile_input['nationality']=='philippine' || $profile_input['nationality']=='fil' || $profile_input['nationality']=='pinoy' || $profile_input['nationality']=='pinay' || $profile_input['nationality']=='philippines' || $profile_input['nationality']=='phil' || $profile_input['nationality']=='fhil' || $profile_input['nationality']=='pnoy'):
			$profile_input['nationality']='Filipino';
		endif;
		$profile_input['pob'] = $profile_data->child_placeofbirth;
		$profile_input['present_address'] = $profile_data->child_city_address;
		$profile_input['contact_no'] = $profile_data->child_telno;
		$profile_input['contact_no'] = $profile_data->mobile_phone;
		$profile_input['religion'] = $profile_data->child_religous;
		$profile_input['living_in_ph_with_father'] = !isset($profile_data->living_in_ph_with_father) ? 'no' : 'yes';
		$profile_input['living_in_ph_with_mother'] = !isset($profile_data->living_in_ph_with_mother) ? 'no' : 'yes';
		/*$profile_input['living_in_ph_with_step_father'] = !isset($profile_data->living_in_ph_with_step_father ) ? 'no' : 'yes';
		$profile_input['living_in_ph_with_step_mother'] = !isset($profile_data->living_in_ph_with_step_mother) ? 'no' : 'yes';*/
		$profile_input['living_in_ph_with_guardian'] = !isset($profile_data->living_in_ph_with_guardian) ? 'no' : 'yes';
		/*$profile_input['living_in_ph_with_legal_guardian_name'] = $profile_data->living_in_ph_with_legal_guardian_name;
		$profile_input['living_in_ph_with_legal_guardian_contact_number'] = $profile_data->living_in_ph_with_legal_guardian_contact_number;*/
		$profile_input['childs_english_reading_writing_level'] = $profile_data->childs_english_reading_writing_level;
		$profile_input['childs_english_verbal_proficiency'] = $profile_data->childs_english_verbal_proficiency;
		$profile_input['language_at_home'] = $profile_data->language_at_home;
		$profile_input['participation_in_advanced_level_classes'] = $profile_data->participation_in_advanced_level_classes;
		$profile_input['remedial_help_in_previous_schools'] = $profile_data->remedial_help_in_previous_schools;
		$profile_input['special_talent_interest_dance'] = !isset($profile_data->special_talent_interest_dance) ? 'no' : 'yes';
		$profile_input['special_talent_interest_drama'] = !isset($profile_data->special_talent_interest_drama) ? 'no' : 'yes';
		$profile_input['special_talent_interest_art'] = !isset($profile_data->special_talent_interest_art) ? 'no' : 'yes';
		$profile_input['special_talent_interest_acomputer'] = !isset($profile_data->special_talent_interest_acomputer) ? 'no' : 'yes';
		$profile_input['special_talent_interest_vocal_chorus'] = !isset($profile_data->special_talent_interest_vocal_chorus) ? 'no' : 'yes';
		$profile_input['special_talent_interest_athletics'] = !isset($profile_data->special_talent_interest_athletics) ? 'no' : 'yes';
		$profile_input['special_talent_interest_athletics_spec'] = $profile_data->special_talent_interest_athletics_spec;
		$profile_input['special_talent_interest_band'] = !isset($profile_data->special_talent_interest_band) ? 'no' : 'yes';
		$profile_input['special_talent_interest_band_spec'] = $profile_data->special_talent_interest_band_spec;
		$profile_input['special_talent_interest_string'] = !isset($profile_data->special_talent_interest_string) ? 'no' : 'yes';
		$profile_input['special_talent_interest_string_spec'] = $profile_data->special_talent_interest_string_spec;
		$profile_input['dhc_hinder_learning_process'] = $profile_data->hinder_childs_learning;
		$profile_input['leave_school'] = $profile_data->leave_school;
		$profile_input['previous_school'] = $profile_data->previous_school;
		$profile_input['previous_school_address'] = $profile_data->previous_school_address;
		$profile_input['previous_grade_level'] = $profile_data->previous_grade_level;
		/*$profile_input['previous_school_grade_1'] =$profile_data->previous_school_grade_1;
		$profile_input['previous_school_name_1'] =$profile_data->previous_school_name_1;
		$profile_input['previous_school_year_1'] =$profile_data->previous_school_year_1;
		$profile_input['previous_school_grade_2'] =$profile_data->previous_school_grade_2;
		$profile_input['previous_school_name_2'] =$profile_data->previous_school_name_2;
		$profile_input['previous_school_year_2'] =$profile_data->previous_school_year_2;
		$profile_input['previous_school_grade_3'] =$profile_data->previous_school_grade_3;
		$profile_input['previous_school_name_3'] =$profile_data->previous_school_name_3;
		$profile_input['previous_school_year_3'] =$profile_data->previous_school_year_3;*/
		$profile_input['guardian_name'] = $profile_data->guardian_name;
		$profile_input['guardian_mname'] = $profile_data->guardian_mname;
		$profile_input['guardian_lname'] = $profile_data->guardian_lname;
		$profile_input['guardian_citizenship'] = $profile_data->guardian_citizenship;
		$profile_input['guardian_visa'] = $profile_data->guardian_visa;
		$profile_input['guardian_occup'] = $profile_data->guardian_occup;
		$profile_input['guardian_office_tel'] = $profile_data->guardian_office_tel;
		$profile_input['guardian_contact_no'] = $profile_data->guardian_contact_no;
		$profile_input['guardian_contact_no_text'] = !isset($data['guardian_contact_no_text']) ? 'no' : 'yes';
		
		
		$profile_input['created'] = NOW;
		
		$this->db->insert('profiles',$profile_input);
		$this->last_insert_id = $this->db->insert_id();
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	private function __format($date)
	{
		$date = DateTime::createFromFormat('Y-m-d', $date);
		$dob = $date->format('Y-m-d');
		$birthday = strtotime($dob);
		$time = time();
		$seconds = $time - $birthday;
		$age = floor($seconds / 31536000);
		return (object)array('dob'=>$dob,'age'=>$age);
	}
	
	public function get_last_insert_id()
	{
		return $this->last_insert_id;
	}
	
	/*
		Check if the email is already used and returns TRUE if verified, FALSE if not verified.
	*/
	public function check_my_mail($emailer,$profile_id)
	{
		$sql = "SELECT e.verified
				FROM enrollments e 
				LEFT JOIN profiles p
				ON e.e_profile_id = p.profile_id
				WHERE p.email = ?
				AND p.profile_id != ? ";
				
		$query = $this->db->query($sql,array($emailer,$profile_id));
		$data = $query->num_rows() >= 1 ? $query->result() : FALSE;
		
		if(!empty($data))
		{
			$count = 0;
			$has = 0;
			foreach($data as $email){
				$has += $email->verified;
				$count++;
			}
			
			if($has >= 1)
			{
				return TRUE;
			}else{
				return FALSE;
			}

		}else{
			return FALSE;
		}
	}
}
?>